import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import '../share/my_color.dart';
import '../ui/appbar/my_appbar.dart';
import './tab_title_widget.dart';

class TabPageWidget extends StatefulWidget {
  const TabPageWidget({
    Key? key,
    required this.titleList,
    required this.pageChildren,
    this.physics,
    this.showAppBar = true,
    this.tabPageType = TabPageType.top, //分页类型，默认在页面顶部
    this.height = 44,
    this.isScrollable = true, //是否可以滚动， 否--平分
    this.onTop, //title 改变-  分页 改变
    this.indicatorWidth = 16.0, //指示器的宽度
    this.indicatorBottom = 6.0, //指示器距离底部的距离
    this.indicatorHeight = 3.0, //指示器的高度
    this.labelPadding = const EdgeInsets.symmetric(horizontal: 16), //tab padding
    this.padding = const EdgeInsets.symmetric(horizontal: 5), //title导航padding
    this.unSelectLabColor = MyColors.color_85_85_85,
    this.labelColor = MyColors.color_35_35_35,
    this.labelStyle = const TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
    this.unSelectLabStyle = const TextStyle(fontSize: 14),
    this.dragStartBehavior = DragStartBehavior.down,

    //导航栏相关设置 showAppBar = true有效  --见 MyAppBar 参数说明
    this.appBarTitle,
    this.appBarTitleColor = MyColors.color_34_34_34,
    this.appBarTitleFont = 18,
    this.appBarTitleFontWeight = FontWeight.w500,
    this.appBarTitleWidget, //navigation 模式下设置无效
    this.appBarLeading,
    this.appBarBackgroundColor = MyColors.color_255_255_255,
    this.appBarElevation = 0, //底部阴影
    this.appBarIconTheme = const IconThemeData(color: MyColors.color_34_34_34, size: 24),
    this.appBarActions,
    this.appBarBottom,
    this.appBarCenterTitle = true,
    this.appBarBrightness = Brightness.light,
    this.appBarTitleSpacing = NavigationToolbar.kMiddleSpacing,
    this.appBarAutomaticallyImplyLeading = true, //隐藏左边返回按钮
    this.appBarBackFunction, //返回按钮被点击
    this.appBarRightFunction,
    this.bottomBtn,
  })  : assert((titleList != null || titleList.length != 0), "titleList不能为空"),
        assert((pageChildren != null || pageChildren.length != 0), "pageChildren不能为空"),
        assert((titleList.length == pageChildren.length), "titleList pageChildren的长度相等"),
        super(key: key);

  final List<Widget> pageChildren;
  final ScrollPhysics? physics;
  final bool showAppBar;
  final TabPageType tabPageType;
  final double height;

  final List<String> titleList;
  final bool isScrollable; //是否可以滑动
  final Function(int value)? onTop;
  final double indicatorWidth;
  final double indicatorBottom;
  final double indicatorHeight;
  final EdgeInsetsGeometry labelPadding;
  final EdgeInsetsGeometry padding;

  final Color labelColor;
  final TextStyle labelStyle;
  final Color unSelectLabColor;
  final TextStyle unSelectLabStyle;
  final DragStartBehavior dragStartBehavior;

  //一下为导航栏设置 --见 MyAppBar
  final String? appBarTitle;
  final Color appBarTitleColor;

  final double appBarTitleFont;

  final FontWeight appBarTitleFontWeight;
  final Widget? appBarTitleWidget;
  final Widget? appBarLeading;
  final Color appBarBackgroundColor;
  final double appBarElevation; //底部阴影
  final IconThemeData appBarIconTheme;

  final List<Widget>? appBarActions;
  final PreferredSizeWidget? appBarBottom;
  final bool appBarCenterTitle;
  final Brightness appBarBrightness;

  final double appBarTitleSpacing;

  final bool appBarAutomaticallyImplyLeading; //隐藏左边返回按钮
  final Function? appBarBackFunction; //返回按钮被点击
  final Function? appBarRightFunction;
  final Widget? bottomBtn;

  @override
  _TabPageWidgetState createState() => _TabPageWidgetState();
}

class _TabPageWidgetState extends State<TabPageWidget> with SingleTickerProviderStateMixin {
  TabController? _tabController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabController = TabController(length: widget.titleList.length, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: widget.showAppBar ? buildAppBar() : null,
      body: Column(
        children: [
          Expanded(
            child: TabBarView(
              children: widget.pageChildren,
              controller: _tabController,
            ),
          ),
          widget.bottomBtn==null?Container():widget.bottomBtn!,
        ],
      ),
    );
  }

  PreferredSizeWidget buildAppBar() {
    bool navigation = TabPageType.navigation == widget.tabPageType;
    return MyAppBar(
      context,
      title: widget.appBarTitle,
      titleColor: widget.appBarTitleColor,
      titleFont: widget.appBarTitleFont,
      titleFontWeight: widget.appBarTitleFontWeight,
      titleWidget: navigation ? getTabWidget(context) : widget.appBarTitleWidget,
      leading: widget.appBarLeading,
      backgroundColor: widget.appBarBackgroundColor,
      elevation: widget.appBarElevation,
      iconTheme: widget.appBarIconTheme,
      actions: widget.appBarActions,
      bottom: widget.tabPageType != TabPageType.top
          ? widget.appBarBottom
          : PreferredSize(
              child: getTabWidget(context),
              preferredSize: Size.fromHeight(widget.height),
            ),
      centerTitle: widget.appBarCenterTitle,
      brightness: widget.appBarBrightness,
      titleSpacing: widget.appBarTitleSpacing,
      automaticallyImplyLeading: widget.appBarAutomaticallyImplyLeading,
      //隐藏左边返回按钮
      backFunction: widget.appBarBackFunction,
      //返回按钮被点击
//      rightFunction: widget.appBarRightFunction,
    ) as PreferredSizeWidget;
  }

  Widget getTabWidget(BuildContext context) {
    return TabTitleWidget(
      titleList: widget.titleList,
      tabController: _tabController,
      isScrollable: widget.isScrollable,
      onTop: widget.onTop,
      indicatorBottom: widget.indicatorBottom,
      indicatorHeight: widget.indicatorHeight,
      indicatorWidth: widget.indicatorWidth,
      labelStyle: widget.labelStyle,
      labelColor: widget.labelColor,
      labelPadding: widget.labelPadding,
      dragStartBehavior: widget.dragStartBehavior,
      height: widget.height,
      width: double.infinity,
      padding: widget.padding,
      unSelectLabColor: widget.unSelectLabColor,
      unSelectLabStyle: widget.unSelectLabStyle,
    );
  }
}

enum TabPageType {
  navigation, //在导航栏上
  top, //在页面底部
}
